package com.liucxu.qiushimeinv.Utils;

import com.liucxu.qiushimeinv.Base.BaseFragment;

import java.util.HashMap;

/**
 * 生成Fragment工具类
 * Created by liucxu on 2017/1/1.
 */

public class ViewUtils {

    private static HashMap<String, BaseFragment> fragments = new HashMap<>();

    public static BaseFragment CreateFragment(Class<?> fragment, boolean add) {
        String className = fragment.getName();
        if (add) {
            BaseFragment returnFragment = null;
            returnFragment = fragments.get(className);
            if (returnFragment == null) {
                try {
                    returnFragment = (BaseFragment) Class.forName(fragment.getName()).newInstance();
                    fragments.put(fragment.getName(), returnFragment);
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
            return fragments.get(fragment.getName());
        } else {
            try {
                return (BaseFragment) Class.forName(fragment.getName()).newInstance();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
